package com.chzc.fm.duotin.db;

import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;

/**
 * Created by BillSinla on 2016/6/30.
 */
public class HistoryManager {
    private static Context mContext;
    private HistoryHelper helper;
    private SQLiteDatabase db;
    private static HistoryManager manager;

    public static HistoryManager getInstance(){
        if(manager == null){
            synchronized (HistoryManager.class){
                if(manager == null){
                    manager = new HistoryManager(mContext);
                }
            }
        }
        return manager;
    }

    public static void init(Context context){
        mContext = context;
    }

    private HistoryManager(Context context){
        helper = new HistoryHelper(context);
        db = helper.getWritableDatabase();
    }

    /**
     * 添加一条播放历史
     */
    public void addHistory(History history){
        db = helper.getWritableDatabase(); //可写的数据操作
        db.insert(HistoryHelper.TABLE_NAME, null, getContentValuesByHistory(history));
        //数据库作完后记得关闭
        db.close();
    }

    public void deleteHistory(History history){
        db = helper.getWritableDatabase(); //可写的数据操作
        db.delete(HistoryHelper.TABLE_NAME, "where"
                +HistoryHelper.HistoryTable.COLUMNS_MP3_URL + " = ?",
                new String[]{history.getMp3Url()});
        db.close();
    }

    public void updateHistory(History history){
        db = helper.getWritableDatabase();
        db.update(HistoryHelper.TABLE_NAME, getContentValuesByHistory(history), "where"
            + HistoryHelper.HistoryTable.COLUMNS_RNAME + " = ?",
                new String[]{history.getRname()});
        db.close();
    }





    private ContentValues getContentValuesByHistory(History history) {
        ContentValues values = new ContentValues();
        values.put(HistoryHelper.HistoryTable.COLUMNS_RNAME, history.getRname());

        return values;
    }


}
